import 'package:flutter/material.dart';
import 'package:intl/intl.dart';

class CustomDatePicker extends StatelessWidget {
  final String? label;
  final DateTime? selectedDate;
  final ValueChanged<DateTime> onDateSelected;
  final bool labelMark;
  final String? hintText;
  final String? format;
  final bool hiddenLabel;
  final FormFieldValidator<DateTime>? validator;
  final DateTime? firstDate;
  final DateTime? lastDate;
  final DateTime? initialDate;
  final InputBorder? border;
  final InputBorder? focusedBorder;
  final InputBorder? errorBorder;
  final EdgeInsetsGeometry? contentPadding;

  const CustomDatePicker({
    super.key,
    this.label,
    required this.selectedDate,
    required this.onDateSelected,
    this.labelMark = false,
    this.hintText,
    this.format,
    this.hiddenLabel = false,
    this.validator,
    this.firstDate,
    this.lastDate,
    this.initialDate,
    this.border,
    this.focusedBorder,
    this.errorBorder,
    this.contentPadding,
  });

  @override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    final formatRule = format ?? 'yyyy/MM/dd';
    final dateFormat = DateFormat(formatRule);

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        if (!hiddenLabel) ...[
          Text.rich(
            TextSpan(
              text: label,
              children: [if (labelMark) TextSpan(text: ' *', style: TextStyle(color: theme.colorScheme.error))],
            ),
            style: theme.textTheme.bodyMedium?.copyWith(
              fontWeight: FontWeight.w500,
              color: theme.colorScheme.onSurface.withOpacity(0.8),
            ),
          ),
          const SizedBox(height: 8),
        ],
        InkWell(
          onTap: () async {
            final date = await showDatePicker(
              context: context,
              initialDate: selectedDate ?? initialDate ?? DateTime.now(),
              firstDate: firstDate ?? DateTime(2000),
              lastDate: lastDate ?? DateTime(2100),
              builder: (context, child) {
                return Theme(
                  data: theme.copyWith(colorScheme: theme.colorScheme.copyWith(primary: theme.colorScheme.primary)),
                  child: child!,
                );
              },
            );
            if (date != null) {
              onDateSelected(date);
            }
          },
          child: InputDecorator(
            decoration: InputDecoration(
              hintText: hintText ?? formatRule,
              hintStyle: theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.onSurface.withOpacity(0.4)),
              border: border ?? const OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(8))),
              enabledBorder:
                  border ??
                  OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: BorderSide(color: theme.colorScheme.outline.withOpacity(0.5)),
                  ),
              focusedBorder:
                  focusedBorder ??
                  OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: BorderSide(color: theme.colorScheme.primary, width: 1.5),
                  ),
              errorBorder:
                  errorBorder ??
                  OutlineInputBorder(
                    borderRadius: BorderRadius.circular(8),
                    borderSide: BorderSide(color: theme.colorScheme.error),
                  ),
              contentPadding: contentPadding ?? EdgeInsets.symmetric(vertical: 14, horizontal: 16),
              suffixIcon: Icon(Icons.calendar_today),
              filled: true,
              fillColor: theme.colorScheme.surfaceContainerHighest.withOpacity(0.4),
              errorText: validator?.call(selectedDate),
              errorStyle: theme.textTheme.bodySmall?.copyWith(color: theme.colorScheme.error, height: 1.2),
            ),
            child:
                selectedDate != null
                    ? Text(dateFormat.format(selectedDate!), style: theme.textTheme.bodyMedium)
                    : Text(
                      hintText ?? formatRule,
                      style: theme.textTheme.bodyMedium?.copyWith(color: theme.colorScheme.onSurface.withOpacity(0.4)),
                    ),
          ),
        ),
      ],
    );
  }
}
